<?php

class Raid_Form_CreateRaid extends Pldkp_Form 
{ 
    
    const DATE_FORMAT = 'yyyy-MM-dd HH:mm';
    
    public function init()
    {   
        
        $this->setName('create_raid');
        $this->addElementPrefixPath('Pldkp_Validate', 'Pldkp/Validate/', 'validate');
        
        
        
        //TODO Change these to some proper jquery date and time elements
        // http://framework.zend.com/manual/en/zendx.jquery.form.html
        // Use: ZendX_JQuery_Form_Element_DatePicker, the form must extend ZendX_JQuery_Form
        // Though we need a Jquery subclass for Pldkp_Form for design elements
        // $startDate = new ZendX_JQuery_Form_Element_DatePicker('start_date', array('helper' => 'jqueryHelperWrapper', 'wrapHelper' => 'datePicker', 'wrapClass' => 'input_large'));
        $startDate = new Zend_Form_Element_Text('start_time', array('helper' => 'helperWrapper', 'wrapHelper' => 'formText', 'wrapClass' => 'input_large'));
        $startDate->setLabel('Raid Start')
            ->setRequired(true)
            ->addValidator('DateTime',false,array(self::DATE_FORMAT));
        
        
        $signupDate = new Zend_Form_Element_Text('end_time', array('helper' => 'helperWrapper', 'wrapHelper' => 'formText', 'wrapClass' => 'input_large'));
        $signupDate->setLabel('Raid Finish')
            ->setRequired(true)
            ->addValidator('DateTime',false,array(self::DATE_FORMAT));
        
        
        $finishDate = new Zend_Form_Element_Text('signup_deadline', array('helper' => 'helperWrapper', 'wrapHelper' => 'formText', 'wrapClass' => 'input_large'));
        $finishDate->setLabel('Signup Deadline')
            ->setRequired(true)
            ->addValidator('DateTime',false,array(self::DATE_FORMAT));
        
        
        $raidLeader = new Zend_Form_Element_Select('raid_leader', array('helper' => 'helperWrapper', 'wrapHelper' => 'formSelect', 'wrapClass' => 'input_large'));
        $raidLeader->setLabel('Raid Leader')
              ->setMultiOptions($this->getRaidLeaders())
              ->setRequired(true)->addValidator('NotEmpty', true);
        
        
        $note = new Zend_Form_Element_Textarea('note', array('helper' => 'helperWrapper', 'wrapHelper' => 'formTextarea', 'wrapClass' => 'input_textarea'));
        $note->setLabel('Note');
        $note->setValue('Please Specify Raid Dungeon');
        
        $submit = new Zend_Form_Element_Submit('submit', array('helper' => 'helperWrapper', 'wrapHelper' => 'formSubmit', 'wrapClass' => 'button_large'));
        $submit->setLabel('Create Raid');
        
        $this->addElements(array($startDate, $signupDate, $finishDate, $raidLeader, $note, $submit));
        
    }
    
    
    
    public function __construct($date = null)
    {
        parent::__construct();
        if ($date == null) $date = new Zend_Date();
        $date->set('20:00:00',Zend_Date::TIMES);
        
        $this->setDefault('start_date',$date->get(Zend_Date::DATE_MEDIUM));
        $this->calcDefaults($date);
    }
    
    
    public function calcDefaults($date)
    {
        
        $start = $date;
        
        $signup = $date->copyPart(null);
        $signup->subDay(1);
        
        $finish = $date->copyPart(null);
        $finish->set('23:00:00',Zend_Date::TIMES);
        
        
        
        $this->setDefaults( array(
                                    'start_time'    => $start->toString(self::DATE_FORMAT),
                                    'end_time'   => $finish->toString(self::DATE_FORMAT),
                                    'signup_deadline'   => $signup->toString(self::DATE_FORMAT)
                            )
        );
    }
    
    /**
     * Gets all the raidleaders sorted alphabetically,
     * for now raidleaders = all users, this we might want to change
     * @return array(user_id => name)
     */
    private function getRaidLeaders()
    {
        $usersTable = new User_Model_Users();
        $select = $usersTable->select()->order('name ASC');
        $usersRowset = $usersTable->fetchAll($select);
        
        $result = array();
        foreach ($usersRowset as $user)
        {
            $result[$user->id] = $user->name;
        }
        return $result;
    }
}